<!-- 我的收藏 -->
 <!--我的收藏-->
<template>
  <div class="collection-container">
    <!-- 筛选栏 -->
    <el-row :gutter="20" class="filter-bar">
      <el-col :span="6">
        <el-input 
          v-model="searchQuery" 
          placeholder="输入关键词搜索"
          clearable
        >
          <template #append>
            <el-button icon="Search" @click="fetchCollections"></el-button>
          </template>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-select v-model="typeFilter" placeholder="全部类型">
          <el-option label="商品" value="product"></el-option>
          <el-option label="文章" value="article"></el-option>
          <el-option label="视频" value="video"></el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-date-picker 
          v-model="dateRange" 
          type="daterange" 
          range-separator="至" 
          start-placeholder="开始日期" 
          end-placeholder="结束日期"
          value-format="YYYY-MM-DD"
        ></el-date-picker>
      </el-col>
      <el-col :span="8" class="text-right">
        <el-button type="primary" @click="fetchCollections">查询</el-button>
        <el-button @click="resetFilters">重置</el-button>
      </el-col>
    </el-row>

    <!-- 收藏列表 -->
    <el-table 
      :data="displayedItems" 
      style="width: 100%" 
      :loading="loading"
      stripe
      empty-text="暂无收藏记录"
    >
      <el-table-column prop="title" label="标题" width="300"></el-table-column>
      <el-table-column label="类型" width="120">
        <template #default="{ row }">
          <el-tag :type="row.type === 'product' ? 'success' : (row.type === 'article' ? 'info' : 'warning')">{{ row.type }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createdAt" label="收藏时间" width="180"></el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="{ row }">
          <el-button size="small" @click="viewDetails(row)">查看详情</el-button>
          <el-popconfirm title="确定要取消收藏吗？" confirm-button-text="确认" cancel-button-text="取消" @confirm="removeCollection(row)">
            <template #reference>
              <el-button size="small" type="danger">取消收藏</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页控件 -->
    <el-pagination 
      background 
      layout="prev, pager, next" 
      :total="total"
      :current-page="currentPage"
      @current-change="handlePageChange"
    ></el-pagination>
  </div>
</template>

<script setup>
import { ref, reactive, computed } from 'vue';
import { ElMessage } from 'element-plus';

const searchQuery = ref('');
const typeFilter = ref('');
const dateRange = ref([]);
const currentPage = ref(1);
const total = ref(0);
const loading = ref(false);

// 示例数据
const collections = [
  {
    id: 1,
    title: '中学王者荣耀课程',
    type: 'product',
    createdAt: '2023-06-19'
  },
  // ... 其他示例数据 ...
];

// 过滤器应用
const filteredItems = computed(() => {
  let filtered = collections;
  if (searchQuery.value) filtered = filtered.filter(c => c.title.includes(searchQuery.value));
  if (typeFilter.value) filtered = filtered.filter(c => c.type === typeFilter.value);
  if (dateRange.value.length) {
    const [start, end] = dateRange.value;
    filtered = filtered.filter(c => c.createdAt >= start && c.createdAt <= end);
  }
  return filtered;
});

// 分页逻辑
const displayedItems = computed(() => {
  return filteredItems.value.slice(
    (currentPage.value - 1) * 10,
    currentPage.value * 10
  );
});

// 页面切换
const handlePageChange = (page) => {
  currentPage.value = page;
};

// 重置筛选
const resetFilters = () => {
  searchQuery.value = '';
  typeFilter.value = '';
  dateRange.value = [];
  fetchCollections();
};

// 模拟数据获取
const fetchCollections = () => {
  loading.value = true;
  setTimeout(() => {
    loading.value = false;
    // 实际应调用API获取数据
    total.value = filteredItems.value.length;
  }, 500);
};

// 查看详情
const viewDetails = (row) => {
  console.log('查看收藏详情:', row);
};

// 取消收藏
const removeCollection = (row) => {
  // 执行取消收藏逻辑
  ElMessage.success('已取消收藏');
};
</script>

<style scoped>
.collection-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.filter-bar {
  margin-bottom: 20px;
}

.el-table {
  margin-bottom: 20px;
}

.text-right {
  text-align: right;
}
</style>